package com.example.myapplication2.adapter;

import android.content.Context;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import com.example.myapplication2.R;
import com.example.myapplication2.model.Task;
import java.util.List;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;

public class TaskAdapter extends RecyclerView.Adapter<TaskAdapter.TaskViewHolder> {
    private Context context;
    private List<Task> tasks;
    private OnTaskCompletedListener listener;

    public interface OnTaskCompletedListener {
        void onTaskCompleted(Task task, boolean isCompleted);
    }

    public TaskAdapter(Context context, List<Task> tasks) {
        this.context = context;
        this.tasks = tasks;
    }

    public void setOnTaskCompletedListener(OnTaskCompletedListener listener) {
        this.listener = listener;
    }

    public void updateTasks(List<Task> newTasks) {
        this.tasks = newTasks;
        notifyDataSetChanged();
    }

    @NonNull
    @Override
    public TaskViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.item_task, parent, false);
        return new TaskViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull TaskViewHolder holder, int position) {
        Task task = tasks.get(position);
        
        holder.tvTaskNumber.setText(task.getTaskNumber());
        holder.tvTaskTitle.setText(task.getTitle());
        
        String createTime = task.getCreateTime();
        if (createTime != null && !createTime.isEmpty()) {
            try {
                SimpleDateFormat inputFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.getDefault());
                SimpleDateFormat outputFormat = new SimpleDateFormat("yyyy年MM月dd日 HH:mm", Locale.getDefault());
                Date date = inputFormat.parse(createTime);
                if (date != null) {
                    createTime = outputFormat.format(date);
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        holder.tvCreateTime.setText(createTime);
        
        holder.cbTaskCompleted.setChecked(task.isCompleted());

        int priorityColor;
        switch (task.getPriority()) {
            case 3:
                priorityColor = Color.parseColor("#FFE0E0");
                break;
            case 2:
                priorityColor = Color.parseColor("#FFF3E0");
                break;
            case 1:
                priorityColor = Color.parseColor("#FFE0B2");
                break;
            case 0:
                priorityColor = Color.parseColor("#E3F2FD");
                break;
            default:
                priorityColor = Color.parseColor("#E3F2FD");
        }
        
        holder.itemView.setBackgroundTintList(android.content.res.ColorStateList.valueOf(priorityColor));

        float alpha = task.isCompleted() ? 0.6f : 1.0f;
        holder.itemView.setAlpha(alpha);

        holder.cbTaskCompleted.setOnCheckedChangeListener((buttonView, isChecked) -> {
            if (listener != null) {
                listener.onTaskCompleted(task, isChecked);
                holder.itemView.setAlpha(isChecked ? 0.6f : 1.0f);
            }
        });
    }

    @Override
    public int getItemCount() {
        return tasks.size();
    }

    static class TaskViewHolder extends RecyclerView.ViewHolder {
        TextView tvTaskNumber;
        TextView tvTaskTitle;
        TextView tvCreateTime;
        CheckBox cbTaskCompleted;

        TaskViewHolder(@NonNull View itemView) {
            super(itemView);
            tvTaskNumber = itemView.findViewById(R.id.tv_task_number);
            tvTaskTitle = itemView.findViewById(R.id.tv_task_title);
            tvCreateTime = itemView.findViewById(R.id.tv_create_time);
            cbTaskCompleted = itemView.findViewById(R.id.cb_task_completed);
        }
    }
} 